package com.example.experminent3;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListActivity extends AppCompatActivity {

    private ListView lvAnimal;
    private List<Map<String, Object>> dataList;
    private String[] animals = {"Lion", "Tiger", "Monkey", "Dog", "Cat", "Elephant"};
    // 这里保留空位，你可以自行添加图片路径
    private int[] images = {
            R.drawable.lion,  // 替换为你的图片1
            R.drawable.tiger,  // 替换为你的图片2
            R.drawable.monkey,  // 替换为你的图片3
            R.drawable.dog,  // 替换为你的图片4
            R.drawable.cat,  // 替换为你的图片5
            R.drawable.elephant   // 替换为你的图片6
    };

    @SuppressLint({"WrongViewCast", "MissingInflatedId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);  // 需要创建对应的布局

        lvAnimal = findViewById(R.id.lv_animal);
        initData();
        setAdapter();
        setListViewListener();
    }

    private void initData() {
        dataList = new ArrayList<>();
        for (int i = 0; i < animals.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("animalName", animals[i]);
            map.put("animalImage", images[i]);
            dataList.add(map);
        }
    }

    private void setAdapter() {
        SimpleAdapter adapter = new SimpleAdapter(
                this,
                dataList,
                R.layout.structural,  // 之前创建的列表项布局
                new String[]{"animalName", "animalImage"},
                new int[]{R.id.tv_animal, R.id.iv_animal}
        );
        lvAnimal.setAdapter(adapter);
    }

    private void setListViewListener() {
        lvAnimal.setOnItemClickListener((parent, view, position, id) -> {
            String animal = animals[position];
            Toast.makeText(this, "你选中了：" + animal, Toast.LENGTH_SHORT).show();
            sendNotification(animal);
        });
    }

    private void sendNotification(String animal) {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "animal_channel",
                    "动物通知渠道",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            manager.createNotificationChannel(channel);
        }

        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notification = new Notification.Builder(this, "animal_channel")
                    .setContentTitle(animal)
                    .setContentText("你查看了" + animal + "的信息~")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .build();
        }
        manager.notify(1, notification);
    }

}